import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/material.dart' hide Colors;

class ScrollControl extends StatefulWidget {
  final ScrollController controller;
  final Widget child;

  const ScrollControl({
    super.key,
    required this.controller,
    required this.child,
  });

  @override
  State<ScrollControl> createState() => _ScrollControlState();
}

class _ScrollControlState extends State<ScrollControl> {
  bool isHover = false;
  bool isScrolledToStart = false;
  bool isScrolledToEnd = false;

  @override
  void initState() {
    widget.controller.addListener(_scrollListener);
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      setState(() {
        isScrolledToStart = widget.controller.position.pixels == 0;
        isScrolledToEnd =
            widget.controller.position.pixels ==
            widget.controller.position.maxScrollExtent;
      });
    });
  }

  @override
  void dispose() {
    widget.controller.removeListener(_scrollListener);
    super.dispose();
  }

  void _scrollListener() {
    setState(() {
      _updateScrollPosition();
    });
  }

  void _updateScrollPosition() {
    isScrolledToStart = widget.controller.position.pixels == 0;
    isScrolledToEnd =
        widget.controller.position.maxScrollExtent == 0 ||
        (widget.controller.position.maxScrollExtent > 0 &&
            widget.controller.position.pixels ==
                widget.controller.position.maxScrollExtent);
  }

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      hitTestBehavior: HitTestBehavior.translucent,
      onEnter: (event) {
        setState(() {
          isHover = true;
          _updateScrollPosition();
        });
      },
      onExit: (event) {
        setState(() {
          isHover = false;
        });
      },
      child: Stack(
        children: [
          widget.child,
          if (isHover)
            Material(
              color: Colors.transparent,
              type: MaterialType.transparency,
              child: LayoutBuilder(
                builder: (context, constraints) {
                  final scrollDistance = constraints.maxWidth * 0.85;
                  return Row(
                    children: [
                      if (!isScrolledToStart)
                        InkWell(
                          child: Container(
                            width: 45,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              color: FluentTheme.of(context)
                                  .navigationPaneTheme
                                  .backgroundColor
                                  ?.withValues(alpha: 0.9),
                            ),
                            child: const Icon(FluentIcons.chevron_left),
                          ),
                          onTap: () {
                            widget.controller.animateTo(
                              widget.controller.offset - scrollDistance,
                              duration: const Duration(milliseconds: 300),
                              curve: Curves.easeInOut,
                            );
                          },
                        ),
                      const Spacer(),
                      if (!isScrolledToEnd)
                        InkWell(
                          child: Container(
                            width: 45,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              color: FluentTheme.of(context)
                                  .navigationPaneTheme
                                  .backgroundColor
                                  ?.withValues(alpha: 0.9),
                            ),
                            child: const Icon(FluentIcons.chevron_right),
                          ),
                          onTap: () {
                            widget.controller.animateTo(
                              widget.controller.offset + scrollDistance,
                              duration: const Duration(milliseconds: 300),
                              curve: Curves.easeInOut,
                            );
                          },
                        ),
                    ],
                  );
                },
              ),
            ),
        ],
      ),
    );
  }
}
